package com.ruoyi.mes.material.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mes.systembasic.domain.SmesProduceSequenceResult;
import com.ruoyi.mes.systembasic.domain.SmesSequenceProduce;
import com.ruoyi.mes.systembasic.mapper.SmesSequenceProduceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.material.mapper.SmesMaterialArriveBillMapper;
import com.ruoyi.mes.material.domain.SmesMaterialArriveBill;
import com.ruoyi.mes.material.service.ISmesMaterialArriveBillService;

/**
 * 采购订单Service业务层处理
 * 
 * @author simpsense
 * @date 2020-12-10
 */
@Service
public class SmesMaterialArriveBillServiceImpl implements ISmesMaterialArriveBillService 
{
    @Autowired
    private SmesMaterialArriveBillMapper smesMaterialArriveBillMapper;

    @Autowired
    private SmesSequenceProduceMapper smesSequenceProduceMapper;
    /**
     * 查询采购订单
     * 
     * @param arriveBillNo 采购订单ID
     * @return 采购订单
     */
    @Override
    public SmesMaterialArriveBill selectSmesMaterialArriveBillById(String arriveBillNo)
    {
        return smesMaterialArriveBillMapper.selectSmesMaterialArriveBillById(arriveBillNo);
    }

    /**
     * 查询采购订单列表
     * 
     * @param smesMaterialArriveBill 采购订单
     * @return 采购订单
     */
    @Override
    public List<SmesMaterialArriveBill> selectSmesMaterialArriveBillList(SmesMaterialArriveBill smesMaterialArriveBill)
    {
        return smesMaterialArriveBillMapper.selectSmesMaterialArriveBillList(smesMaterialArriveBill);
    }

    /**
     * 新增采购订单
     * 
     * @param smesMaterialArriveBill 采购订单
     * @return 结果
     */
    @Override
    public int insertSmesMaterialArriveBill(SmesMaterialArriveBill smesMaterialArriveBill)
    {
        smesMaterialArriveBill.setCreateTime(DateUtils.getNowDate());
        smesMaterialArriveBill.setCreateBy(SecurityUtils.getUsername());
        SmesSequenceProduce smesSequenceProduce=new SmesSequenceProduce();
        smesSequenceProduce.setBillType("采购订单");
        smesSequenceProduce.setBillPrefix("PB");
        smesSequenceProduce.setUserName(SecurityUtils.getUsername());
        SmesProduceSequenceResult geBillNo=smesSequenceProduceMapper.generateSequence(smesSequenceProduce);
        if(geBillNo==null||geBillNo.getCode()==null||!geBillNo.getCode().equals("OK")){
            return 0;
        }
        smesMaterialArriveBill.setArriveBillNo(geBillNo.getMsg());
        return smesMaterialArriveBillMapper.insertSmesMaterialArriveBill(smesMaterialArriveBill);
    }

    /**
     * 修改采购订单
     * 
     * @param smesMaterialArriveBill 采购订单
     * @return 结果
     */
    @Override
    public int updateSmesMaterialArriveBill(SmesMaterialArriveBill smesMaterialArriveBill)
    {
        smesMaterialArriveBill.setUpdateTime(DateUtils.getNowDate());
        smesMaterialArriveBill.setUpdateBy(SecurityUtils.getUsername());
        if(smesMaterialArriveBill.getBillStatus().equals("3"))//收货完成
        {
            smesMaterialArriveBill.setCompleteTime(DateUtils.getNowDate());
            smesMaterialArriveBill.setCompleteBy(SecurityUtils.getUsername());
        }
        return smesMaterialArriveBillMapper.updateSmesMaterialArriveBill(smesMaterialArriveBill);
    }

    /**
     * 批量删除采购订单
     * 
     * @param arriveBillNos 需要删除的采购订单ID
     * @return 结果
     */
    @Override
    public int deleteSmesMaterialArriveBillByIds(String[] arriveBillNos)
    {
        return smesMaterialArriveBillMapper.deleteSmesMaterialArriveBillByIds(arriveBillNos);
    }

    /**
     * 删除采购订单信息
     * 
     * @param arriveBillNo 采购订单ID
     * @return 结果
     */
    @Override
    public int deleteSmesMaterialArriveBillById(String arriveBillNo)
    {
        return smesMaterialArriveBillMapper.deleteSmesMaterialArriveBillById(arriveBillNo);
    }
}
